package org.geomesa.example.data;

import org.geotools.data.Query;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.filter.text.ecql.ECQL;
import org.locationtech.geomesa.utils.interop.SimpleFeatureTypes;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.io.WKTReader;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory2;
import org.geotools.factory.CommonFactoryFinder;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;

public class GlobalGridPolygonData implements TutorialData {

    private SimpleFeatureType sft = null;
    private List<SimpleFeature> features = null;
    private List<Query> queries = null;
    private Filter subsetFilter = null;

    @Override
    public String getTypeName() {
        return "custom_polygon";
    }

    @Override
    public SimpleFeatureType getSimpleFeatureType() {
        if (sft == null) {
            String spec = "geom:Polygon:srid=4326,name:String";
            sft = SimpleFeatureTypes.createType(getTypeName(), spec);
        }
        return sft;
    }

    @Override
    public List<SimpleFeature> getTestData() {
        if (features == null) {
            features = new ArrayList<>();
            GeometryFactory geometryFactory = new GeometryFactory();
            WKTReader wktReader = new WKTReader(geometryFactory);

            try (
                InputStream is = getClass().getClassLoader().getResourceAsStream("global_grid.csv");
                InputStreamReader isr = new InputStreamReader(is, java.nio.charset.StandardCharsets.UTF_8);
                CSVParser parser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(isr)
            ) {
                SimpleFeatureBuilder builder = new SimpleFeatureBuilder(getSimpleFeatureType());
                for (CSVRecord record : parser) {
                    String wkt = record.get("geom");
                    String name = record.get("name");
                    builder.set("geom", wktReader.read(wkt));
                    builder.set("name", name);
                    features.add(builder.buildFeature(name));
                }
            } catch (Exception e) {
                throw new RuntimeException("Error reading global_grid.csv", e);
            }
        }
        return features;
    }

    @Override
    public List<Query> getTestQueries() {
        if (queries == null) {
            try {
                queries = new ArrayList<>();
                // 示例：查询经纬度在[0,40]和[0,40]范围内的网格
                String cql = "BBOX(geom, 0, 5, 40, 50)";
                queries.add(new Query(getTypeName(), ECQL.toFilter(cql)));
            } catch (Exception e) {
                throw new RuntimeException("Error creating test queries", e);
            }
        }
        return queries;
    }

    @Override
    public Filter getSubsetFilter() {
        if (subsetFilter == null) {
            FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
            // 示例：创建一个名称以"Grid"开头的网格的子集过滤器
            subsetFilter = ff.like(ff.property("name"), "Grid%", "*", "?", "\\");

        }
        return subsetFilter;
    }
}